package test;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import dsa.LinkedList;
import dsa.ListIterator;

@SuppressWarnings("javadoc")
public class AdamWentzelTest
{
	LinkedList	list, list2;

	@Before
	public void setUp() throws Exception
	{
		list = new LinkedList();
		list.addLast(2);
		list.addLast(3);
		list.addLast(4);
		list.addLast(5);
		list.addFirst(1);
		list.addFirst(0);
	}

	@After
	public void tearDown() throws Exception
	{
		list = null;
	}

	@Test
	public void testSize()
	{
		assertEquals(6, list.size());
		list.removeFirst();
		assertEquals(5, list.size());
		list.removeFirst();
		assertEquals(4, list.size());
		list.removeFirst();
		assertEquals(3, list.size());
		list.removeFirst();
		assertEquals(2, list.size());
		list.removeFirst();
		assertEquals(1, list.size());
		list.removeFirst();
		assertEquals(0, list.size());
	}

	@Test
	public void testGets()
	{
		assertTrue(list.getFirst().equals(0));
		assertEquals(5, list.getLast());
		assertEquals(3, list.get(3));
		list.remove(5);
		assertEquals(4, list.getLast());
		list.addLast(5);
		assertEquals(5, list.getLast());
	}

	@Test
	public void testRemove()
	{
		assertEquals(0, list.remove(0));
		list.addFirst(0);
		assertEquals(0, list.removeFirst());
		assertEquals(5, list.removeLast());
		list.addLast(5);
		assertEquals(2, list.remove(1));
		assertEquals(1, list.remove(0));
		list.addFirst(99);
		assertEquals(99, list.remove(0));
	}

	@Test
	public void testIndexOf()
	{
		assertEquals(0, list.indexOf(0));
		assertEquals(1, list.indexOf(1));
		assertEquals(2, list.indexOf(2));
		assertEquals(3, list.indexOf(3));
		assertEquals(4, list.indexOf(4));
		assertEquals(-1, list.indexOf(555));
		list.remove(2);
		assertEquals(1, list.indexOf(1));
		list.addFirst(0);
		assertEquals(0, list.indexOf(0));
	}

	@Test
	public void testContains()
	{
		assertTrue(list.contains(1));
		assertFalse(list.contains(111111));
	}

	@Test
	public void testSet()
	{
		assertEquals(1, list.get(1));
		assertEquals(1, list.set(1, "Q"));
		assertEquals("Q", list.get(1));
		assertEquals(1, list.indexOf("Q"));
	}

	@Test
	public void testCloneArray()
	{
		LinkedList clone = (LinkedList) list.clone();
		for (int i = 0; i < list.size(); i++)
			assertTrue(list.get(i).equals(clone.get(i)));
		Object[] arg = list.toArray();
		Object[] arg2 = {0, 1, 2, 3, 4, 5};
		for (int i = 0; i < arg.length; i++)
			assertEquals(arg[i], arg2[i]);
	}

	/*@Test
	public void testEqual() throws Exception
	{
		tearDown();
		list = new LinkedList();
		list.addLast(2);
		list.addLast(3);
		list.addLast(4);
		list.addLast(5);
		list.addFirst(1);
		list.addFirst(0);
		list2 = new LinkedList();
		list2.addLast(2);
		list2.addLast(3);
		list2.addLast(4);
		list2.addLast(5);
		list2.addFirst(1);
		list2.addFirst(0);
		assertTrue(list.equals(list2));
		list2.removeFirst();
		assertFalse(list.equals(list2));
		list2.addLast(0);
		assertFalse(list.equals(list2));
		list2.removeLast();
		list2.addFirst(0);
		assertTrue(list.equals(list2));
	}*/

	@Test
	public void testIteratorAdd()
	{
		ListIterator itter = list.listIterator(0);
		itter.add("a");
		assertEquals("a", list.getFirst());
		itter.next();
		itter.next();
		itter.add("b");
		assertEquals("b", list.get(3));
	}

	@Test
	public void testIteratorNextHasNext()
	{
		ListIterator itter = list.listIterator(0);
		int i = 0;
		while (itter.hasNext())
		{
			assertEquals(i, itter.nextIndex());
			assertEquals(i, itter.next());
			i++;
		}
	}

	@Test
	public void testIteratorPrevHasPrev()
	{
		ListIterator itter = list.listIterator(list.size());
		int i = list.size() - 1;
		while (itter.hasPrevious())
		{
			assertEquals(i, itter.previousIndex());
			assertEquals(i, itter.previous());
			i--;
		}
	}

	@Test
	public void testIterRemove()
	{
		ListIterator itter = list.listIterator(0);
		itter.next();
		itter.remove();
		assertEquals(1, list.getFirst());
		itter.remove();
		assertEquals(1, list.getFirst());
	}
	
	@Test
	public void testItterSet()
	{
		ListIterator itter = list.listIterator(0);
		itter.next();
		itter.set("QQQ");
		assertEquals("QQQ", list.getFirst());
	}
}
